﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ctrlPageSwitch : System.Web.UI.UserControl
{
    #region VARIABLES

    private int categoryId;
    private int visiblePageCount;
    private int sort;

    #endregion VARIABLES

    #region METHODS

    public void SetProperty(int categoryId, int currentIndex, int lastIndex, int visibleCount, int sort)
    {
        this.categoryId = categoryId;
        this.visiblePageCount = visibleCount;
        this.sort = sort;

        int oneSidePage = (visiblePageCount - 1) / 2;
        int startIndex = currentIndex - oneSidePage;

        if (startIndex < 1)
        {
            startIndex = 1;
        }

        StringBuilder sb = new StringBuilder();
        HtmlTextWriter writer = new HtmlTextWriter(new StringWriter(sb));

        if (startIndex != currentIndex)
        {
            AddNewLink(writer, "startPage", 1, "<<", false);
            AddNewLink(writer, "prevPage", currentIndex - 1, Resources.Language.PreviousF, false);
        }
        int index = 0;
        // if ((startIndex + visibleCount - 1) > lastIndex) startIndex--;
        for (int i = 0; i < visibleCount; i++)
        {
            index = startIndex + i;
            if (index > lastIndex) break;
            AddNewLink(writer, "page" + i.ToString(), index, index.ToString(), index == currentIndex);
        }
        if (lastIndex != currentIndex)
        {
            AddNewLink(writer, "nextPage", currentIndex + 1, Resources.Language.NextF, false);
            AddNewLink(writer, "endPage", lastIndex, ">>", false);
        }

        ctrlPageSwitchBody.InnerHtml = sb.ToString();
    }

    private void AddNewLink(HtmlTextWriter writer, string id, int pageNumber, string text, bool current)
    {
        writer.AddAttribute(HtmlTextWriterAttribute.Id, id);
        writer.AddAttribute(HtmlTextWriterAttribute.Href, "" + categoryId.ToString() + "," + pageNumber + "," + sort.ToString() + "," + Resources.Language.Category.ToLower() + ".aspx");
        
        if (current)
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "pageSwitchCurrent");
        else
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "pageSwitchDigits");

        writer.RenderBeginTag(HtmlTextWriterTag.A);
        writer.Write(text);
        writer.RenderEndTag();
    }

    #endregion METHODS

    #region PAGE EVENTS

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    #endregion PAGE EVENTS
}
